package com.csair.youth.dao.impl.hibernate3;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.csair.youth.database.User;

/**
 * Impl:auto created by daobuilder1.1.1 
 * @version 1.0
 * @author daobuilder@csair.com
 */
public class UserDaoImpl extends com.csair.youth.dao.impl.hibernate3.base.UserDaoImplBase{
	private static final String GET_USER_BY_LOGIN_NAME_HQL = "from "+User.REF+" a where a."+User.PROP_LOGIN_NAME+"=:loginName";
	private static final String GET_USER_BY_ROLE_ID_HQL = "from "+User.REF+" a where a."+User.PROP_ROLE_ID+"=:roleId";
	private static final String GET_USER_IN_ROLES_ID_HQL = "from "+User.REF+" a where a."+User.PROP_ROLE_ID+" in ";
	
	public User getUserByLoginName(String loginName){
		Map param = new HashMap();
		param.put("loginName", loginName);
		return this.findFirst(this.getGenericDao().find(GET_USER_BY_LOGIN_NAME_HQL, param, null));
	}
	
	public List getUsersByRoleId(String roleId){
		Map param = new HashMap();
		param.put("roleId", roleId);
		return this.getGenericDao().find(GET_USER_BY_ROLE_ID_HQL, param, null);
	}
	
	public List getUsersInRoles(String roleIds[]){
		return this.getGenericDao().find(GET_USER_IN_ROLES_ID_HQL+buildInClause(roleIds));
	}
	
	private String buildInClause(String roleIds[]){
		if(roleIds == null || roleIds.length == 0){
			return "";
		}
		StringBuffer sb = new StringBuffer("(");
		for(int i=0; i<roleIds.length; i++){
			sb.append("'"+roleIds[i]+"'");
			if(i < roleIds.length-1){
				sb.append(",");
			}
		}
		sb.append(")");
		String clause = sb.toString();
		return clause;
	}
	
	public void resetPasswordById(String id, String password){
		User u = this.findById(id);
		if(u != null){
			u.setPassword(password);
			this.update(u);
		}
	}
}